package com.zhidou;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

public class Bricks implements Serializable {
    private static final long serialVersionUID = -4116815059515723993L;
    private List<Brick> mBricks;
    public final static String TAG = "Bricks";

    public Bricks() {
	mBricks = new ArrayList<Brick>();
	generateBricks();
    }

    public void generateBricks() {
	mBricks.clear();
	Random ran = new Random();
	for (int i = 0; i < 11; i++) {
	    Brick brick = new Brick();
	    brick.leftX = 1 + ran.nextInt(TileView.mXTileCount - 2);

	    if (TileView.mXTileCount - brick.leftX < 5)
		brick.leftX = TileView.mXTileCount - 5;

	    int rLen = ran.nextInt(6);
	    if (rLen < 4)
		rLen = 4;

	    brick.rightX = brick.leftX + rLen > TileView.mXTileCount - 2 ? TileView.mXTileCount - 2 : brick.leftX + rLen;
	    brick.y = 1 + ran.nextInt(TileView.mYTileCount - 2);
	    mBricks.add(brick);
	}
    }

    public boolean checkCollide(Ball ball) {
	for (Brick brick : mBricks) {
	    if (ball.getX() >= brick.leftX && ball.getX() <= brick.rightX && ball.getY() == brick.y) {
		mBricks.remove(brick);
		ball.turnY();
		return true;
	    }
	}
	return false;
    }

    public List<Brick> getBrickList() {
	return mBricks;
    }

    class Brick implements Serializable {
	private static final long serialVersionUID = 7838323031525925937L;
	int leftX;
	int rightX;
	int y;

	public String toString() {
	    StringBuilder sb = new StringBuilder();
	    sb.append(leftX);
	    sb.append(",");
	    sb.append(rightX);
	    sb.append(",");
	    sb.append(y);

	    return sb.toString();
	}
    }
}
